package com.taotao.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.pojo.TaotaoResult;
import com.taotao.pojo.TbContentCategory;
import com.taotao.service.ContentCategoryService;

@Controller
@RequestMapping("/content/category")
public class ContentCategoryController {
	@Autowired 
	private ContentCategoryService contentCategoryService;
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/list")
	@ResponseBody
	public List getContentCategoryList(@RequestParam(value="id",defaultValue="0")long parentId){
		List<TbContentCategory> list=contentCategoryService.getContentCategoryList(parentId);
		List result=new ArrayList<>();
		for(TbContentCategory tcc:list){
			Map node=new HashMap();
			node.put("id", tcc.getId());
			node.put("text", tcc.getName());
			//如果是父节点的话就设置成关闭状态，如果是叶子节点就是open状态
			node.put("state", tcc.getIsParent()?"closed":"open");
			result.add(node);
		}
		return result;
	}
	@RequestMapping("/create")
	@ResponseBody
	public TaotaoResult insertContentCategory(long parentId,String name){
		return contentCategoryService.insertContentCategory(parentId, name);
	}
}
